<?php namespace App\Admin\Access\Role;

use App\Core\Model as BaseModel;

class Model extends BaseModel {

    protected $table = 'sys_role';
    protected $primaryKey = 'role_id';
    
    //获取扩展信息
	public function getExtension($key, $default = ''){
		if(!$this->extension){
			return $default;
		}
		$extension = json_decode($this->extension, true);
		return isset($extension[$key]) ? $extension[$key] : $default;
	}
    
    //获取操作权限
    public function getOperationIds(){
        return explode(',', $this->operation_ids);
    }
    
    //获取数据权限
	public function getDataIdsByGroupId($groupId, $default = []){
		if(!$this->extension){
			return $default;
		}
		$extension = $this->decodeExtension($this->extension);
		return isset($extension['_'.$groupId]) ? $extension['_'.$groupId] : $default;
	}
    
    //编码扩展信息
	public function decodeExtension($data){
        return json_decode($data, true);
	}
	
	//编码扩展信息
	public function encodeExtension($data){
		return json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
	}
}